package com.hd.service.code.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.hd.dao.BaseDao;
import com.hd.entity.code.CodeCategory;
import com.hd.service.code.CodeCategoryService;
import com.hd.util.StringUtils;
import com.hd.util.result.PageInfo;
/**
* 数据类别
*/
@Service("codeCategoryService")
public class CodeCategoryServiceImpl implements CodeCategoryService {
    @Autowired
    private BaseDao<CodeCategory,String>codeCategoryDao;
    @Override
    public void save(CodeCategory codeCategory) {
    	codeCategory.setId(UUID.randomUUID().toString());
       codeCategoryDao.save(codeCategory);
    }
    @Override
    public void delete(String id){
      codeCategoryDao.delete(CodeCategory.class,id);
    }
       

    @Override
    public void update(CodeCategory codeCategory) {
        CodeCategory vo=codeCategoryDao.get(CodeCategory.class,codeCategory.getId());
        codeCategoryDao.update(codeCategory);
    }
    @Override
    public CodeCategory get(String id){
    	return codeCategoryDao.get(CodeCategory.class, id);
    }
        

    @Override
    public void selectDataGrid(PageInfo pageInfo) {
        String hql="from CodeCategory where 1=1 ";
        Map<String,Object>condition=pageInfo.getCondition();
        List<Object> params=new ArrayList<Object>();
        if(!pageInfo.getSort().equals("")) {
            hql+=" order by "+pageInfo.getSort()+" "+pageInfo.getOrder();
        }
        List<CodeCategory> codeCategorys=codeCategoryDao.findPage(hql, pageInfo.getNowpage(), pageInfo.getPagesize(),params);
        pageInfo.setRows(codeCategorys);
        int total=codeCategoryDao.findByList(hql,params).size();
        pageInfo.setTotal(total);
    }

	@Override
	public List<CodeCategory> findAll() {
		String hql="from CodeCategory";
		return codeCategoryDao.find(hql);
	}

	@Override
	public boolean validCategory(String category) {
		boolean b=true;//true-可用，false-占用
        if(StringUtils.isNotNull(category)) {
            String hql="from CodeCategory where category=?";
            List<CodeCategory>codeCategoryList=codeCategoryDao.find(hql,category);
            if(codeCategoryList.size()>0) {
                b=false;
            }
        }
        return b;
	}

	@Override
	public CodeCategory getBycategory(String category) {
		 String hql="from CodeCategory where category=?";
         List<CodeCategory>codeCategoryList=codeCategoryDao.find(hql,category);
         if(codeCategoryList.size()>0) {
             return codeCategoryList.get(0);
         }
		return null;
	}



}
